//
//  SpinnerViewController.m
//  DotAnimation
//
//  Created by ShiMac on 15/6/13.
//  Copyright (c) 2015年 guoyan. All rights reserved.
//

#import "SpinnerViewController.h"

@interface RotationView : UIView


-(void)startAnimation;


@end


@interface RotationView()
{
    CAShapeLayer *shapeLayer;

}

@end



@implementation RotationView


-(id)initWithFrame:(CGRect)frame
{
    
    if (self=[super initWithFrame:frame]) {
        
        
        
        shapeLayer=[CAShapeLayer layer];
        shapeLayer.strokeColor=[UIColor whiteColor].CGColor;
        shapeLayer.lineWidth=1;
        shapeLayer.fillColor=[UIColor clearColor].CGColor;
        shapeLayer.path=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.width)].CGPath;
        shapeLayer.frame=self.bounds;
        shapeLayer.strokeEnd=0;
        shapeLayer.strokeStart=0;
        [self.layer addSublayer:shapeLayer];
        
        
        
    }
    
    
    return self;
    
}

-(void)startAnimation
{
    
    CABasicAnimation *animation1 = [CABasicAnimation animation];
    animation1.keyPath = @"transform.rotation";
    animation1.duration = 4.f;
    animation1.fromValue = @(0.f);
    animation1.toValue = @(2 * M_PI);
    animation1.repeatCount = 10000;
    animation1.removedOnCompletion=NO;
    [shapeLayer addAnimation:animation1 forKey:@"dd444"];
    
    CABasicAnimation *base1=[CABasicAnimation animationWithKeyPath:@"strokeStart"];
    base1.duration=1;
    base1.fromValue=0;
    base1.toValue=@(.3f);
    base1.repeatCount=100;
    base1.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    CABasicAnimation *base2=[CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    base2.duration=1;
    base2.fromValue=@(0);
    base2.toValue=@(1.f);
    base2.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    
    CABasicAnimation *base3=[CABasicAnimation animationWithKeyPath:@"strokeStart"];
    base3.duration=0.5;
    base3.beginTime=1;
    base3.fromValue=@(0.3);
    base3.toValue=@(1);
    base3.repeatCount=100;
    base3.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];


    CABasicAnimation *base4=[CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    base4.duration=0.5;
    base4.beginTime=1;
    base4.fromValue=@(1);
    base4.toValue=@(1.f);
    base4.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];


    
    
    CAAnimationGroup *group=[CAAnimationGroup animation];
    
    group.duration=1.5;
    group.animations=@[base1,base2,base3,base4];
    group.repeatCount=100;
    group.removedOnCompletion=NO;
    group.fillMode=kCAFillModeForwards;
    
    
    
    
    
    [shapeLayer addAnimation:group forKey:@"dd"];
    
    
    
}



@end






@interface SpinnerViewController ()

@end

@implementation SpinnerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
            self.view.backgroundColor=[UIColor colorWithRed:237/255.0f green:85/255.0f blue:101/255.0f alpha:1.0f];

    RotationView *view=[[RotationView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
    [self.view addSubview:view];
    
    
    [view startAnimation];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
